<?php

class Login extends Action{
	
	//显示用户登录表单
	public function index(){
		$this->display("index");
		exit();
	}
	
	//获取验证码信息
	public function getcode(){
		echo new Vcode();
	}
	
	//处理登录的操作
	public function prologin(){
	
		//校验验证码
		if($_POST['mycode']!=$_SESSION['code']){
			$this->assign("errorinfo","验证码输入错误");
			$this->assign('list',$_POST);
			$this->display("index");
			exit();
		}	
		if($_POST['chrole']==3){$tables=D("promulgator");}
		if($_POST['chrole']==1){$tables=D("atelier");}
		if($_POST['chrole']==2){$tables=D("student");}
		
		$data["username"]=$_POST['username'];
		$data["pass"]=$_POST['pass'];
		
		//查询出表中所有名字
		$namelist=$tables->field("username")->select();
		foreach($namelist as $v){
			$chname[]=$v['username'];
		}
		//先判断账号是否存在
		if(!in_array($data["username"],$chname)){
			$this->assign("errorinfo","账号不存在");
			$this->assign('list',$_POST);
			$this->display("index");
			exit();
		}
		
		//判断账号密码是否匹配
		$user = $tables->where($data)->find();		
		//判断是否成功
		if($user){
			//登录成功！将当前用户信息放入到SESSION中		
			$_SESSION['loginuser']=$user;
			$this->redirect("procenter/index"); //跳转到发布者个人中心页 （测试）
		}else{
			$this->assign("errorinfo","密码错误！");
			$this->assign('list',$_POST);
			$this->display("index");
			exit();
		}

	}
	
	//处理用户退出操作
	public function logout(){
		unset($_SESSION['loginuser']);
		$this->display("index");
		exit;
	}
	
	//显示用户注册页
	public function register(){
		$this->display();
		exit();
	}
	
	//检查用户名称是否唯一
	public function unique(){
		$name=$_POST['username'];
		$pro=D("promulgator");
		$ate=D("atelier");
		$stu=D("student");
		
		//对名字进行验证唯一性
		$prolist=$pro->field("username")->select();
		foreach($prolist as $plist){
			$nplist[]=$plist['username'];
		}
		if(in_array($name,$nplist)){
			echo "用户名已存在";
			exit();
		}
		
		$atelist=$ate->field("username")->select();
		foreach($atelist as $alist){
			$nalist[]=$alist['username'];
		}
		if(in_array($name,$nalist)){
			echo "用户名已存在";
			exit();
		}
		
		$stulist=$stu->field("username")->select();
		foreach($stulist as $slist){
			$nslist[]=$slist['username'];
		}
		if(in_array($name,$nslist)){
			echo "用户名已存在";
			exit();
		}
		
		//对名字格式进行验证
        if(preg_match("/^[0-9a-zA-Z_]{6,12}$/i",$name)==1){
            echo "yes";
        }else{
            echo "no";
        }
        exit();
	}
	
	//验证邮箱是否正确
    public function yanmail(){
        $mail=$_POST['mail'];
		if(preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i",$mail)==1){
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}		
	//验证用户输入的验证码,正确就执行添加
	public function vcode(){	
		
		//校验验证码
		if($_POST['usercode']!=$_SESSION['code']){
			$this->assign("errinfo","验证码输入错误");
			$this->assign('list',$_POST);
			$this->display("register");
			exit();
		}

		//获得注册用户的账号，密码，邮箱
		$regarr['username']=$_POST['username'];
		$regarr['pass']=md5($_POST['pass']);
		$regarr['email']=$_POST['email'];
					
		if($_POST['chrole']==3){$tables=D("promulgator");}
		if($_POST['chrole']==1){$tables=D("atelier");}
		if($_POST['chrole']==2){$tables=D("student");}
		
		//执行添加
		$add=$tables->insert($regarr);
		
		if($add){
			$this->success("注册成功",1,"index"); //跳转到登陆页面
		}else{
			$this->error("注册失败",1,"register");
		}
	}

	
}